﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EntModApp.EntServices.IoC
{
  public static class ServiceLocator
  {
    private static Lazy<IServiceLocator> locatorProvider;

    public static IServiceLocator Current 
    {
      get
      {
        if (locatorProvider == null)
          throw new InvalidOperationException("Service Locator Provider has not been set");
        return locatorProvider.Value;
      }
    }

    public static void SetLocatorProvider(IServiceLocator locator)
    {
      if (locator == null)
        throw new ArgumentNullException("locator");
      SetLocatorProvider(new Lazy<IServiceLocator>(() => locator));
    }

    public static void SetLocatorProvider(Func<IServiceLocator> locatorConstructor)
    {
      if (locatorConstructor == null)
        throw new ArgumentNullException("locator");
      SetLocatorProvider(new Lazy<IServiceLocator>(locatorConstructor));
    }

    public static void SetLocatorProvider(Lazy<IServiceLocator> lazyLocator)
    {
      if (lazyLocator == null)
        throw new ArgumentNullException("lazyLocator");
      if (locatorProvider != null)
        throw new InvalidOperationException("Service Locator Provider is already set");
      locatorProvider = lazyLocator;
    }
  }
}
